from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from drinker.forms import RegistrationForm
from django.contrib.auth.models import User

def DrinkerRegistration(request):
        if request.user.is_authenticated():
                return HttpResponseRedirect('/profile')
        if request.method == 'POST':
                form = RegistrationForm(request.POST)
                if form.is_valid():
                        user = User.objects.create_user(username=form.clean_data['username'],email = form.clean_data['email'], password=form.clean_data['password'])
                        user.save()
                        drinker = user.get_profile()
                        drinker.name = form.clean_data['name']
                        drinker.birthday = form.clean_data['birthday']
                        drinker.save()
                        return HttpResponseRedirect('/profile')
                else:
                        return render_to_response('register.html', {'form': form},context_instance=RequestContext(request))
        else:
                '''user is not submitting the form, show them a blank registration form'''
                form = RegistrationForm()
                context ={'form': form}
                return render_to_response('register.html', context,context_instance=RequestContext(request))